package admin

import (
	"VirtualCamera/common"
	"VirtualCamera/controllers"
	"VirtualCamera/models"
	"VirtualCamera/response"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type LoginController struct {
	controllers.BaseController
}

//func (con LoginController) Index(c *gin.Context) {
//	response.Success(c, gin.H{"username": 11}, "登录成功")
//	// c.HTML(http.StatusOK, "admin/login/login.html", gin.H{})
//}

func (con LoginController) DoLogin(c *gin.Context) {
	db := common.GetDB()

	// username := c.PostForm("username")
	// password := c.PostForm("password")
	var requestUser = models.UserInfo{}
	c.Bind(&requestUser)

	telephone := requestUser.Telephone
	password := requestUser.Password

	fmt.Println("postName is ", telephone)
	fmt.Println("postPsd is", password)

	// flag := (username == "admin") && (password == "123456")

	var user models.User
	db.Where("telephone = ? AND password = ?",telephone, password).Find(&user)

	var flag bool
	if user.ID == 0 {
		flag = false
	} else {
		flag = true
	}

	if flag {
		//保存此session
		// session := sessions.Default(c)
		// session.Set("username", username)
		// session.Save()

		// con.Success(c, "登录成功", "/")
		response.Success(c, gin.H{"telephone": telephone,"id":user.ID,"name":user.Name,"email":user.Email,
			"age":user.Age,"interested_games":user.InterestedGames,"collected_games":user.CollectedGames}, "登录成功")
	} else {
		fmt.Print("登陆失败")
		response.Response(c, http.StatusOK, 400, nil, "登陆失败")
		// con.Error(c, "登陆失败 账号密码错误", "/admin/login")
	}
}

//func (con LoginController) LoginOut(c *gin.Context) {
//	c.String(200, "登出界面")
//}
